<template>
	<div class="decorate-imgNav" :style="this.styleObj">
    <div class="padding-box" :class="[(tplItemData.isBorderRadius == 1 || !tplItemData.isBorderRadius )?'borderRadius':'']">
      <div v-if="tplItemData.layout1_style==2 && tplItemData.layout==1" class='style2 img-nav-box'>
        <a v-for="(item, index) in navList" :key="index" href="javascript:;" @click="openLink(item.link)">
          <em :ref="'getWidth'+index" :style="{background: 'url('+item.pic+')',backgroundSize:'cover',height:itemHeight}"></em>
          <p v-if="item.showtitle" :style="{color:item.titleColor}">{{item.showtitle}}</p>
        </a>
      </div>
      <div v-else class='style1 img-nav-box'>
        <div v-if="tplItemData.layout==1" class="layout-box1 layout-box">
          <template v-if="tplItemData.layout1_style==1">
            <a  :ref="'getWidth'+index" v-for="(item, index) in navList" :key="index" @click="openLink(item.link)" :style="{background: 'url('+item.pic+')',backgroundSize:'100%',height:itemHeight}">
              <p v-if="item.showtitle" :style="{color:item.titleColor,background:item.titleBackgroundColor?item.titleBackgroundColor:'#FE9303'}">{{item.showtitle}}</p>
            </a> 
          </template>
          <template v-else-if="tplItemData.layout1_style==3">
            <a  :ref="'getWidth'+index" v-for="(item, index) in navList" :key="index" @click="openLink(item.link)">
              <div class="nav-new"  :style="{background: 'url('+item.pic+')top center no-repeat',backgroundSize:'90% 90%',width:itemHeight,height:itemHeight}"></div>
              <div style="text-align:center" v-if="item.showtitle" :style="{color:item.titleColor,background:item.titleBackgroundColor?item.titleBackgroundColor:'#FE9303'}">{{item.showtitle}}</div>
            </a> 
          </template>
        </div>
        <div v-else class="layout-box2 layout-box">
          <a :ref="'getWidth'+index" v-for="(item, index) in navList" :key="index" href="javascript:;" @click="openLink(item.link)" :class="tplItemData.hasImgMargin==2?'':'marginRight'">
              <img :src="item.pic" :show-menu-by-longpress="true" alt="" mode="widthFix">
            <p v-if="item.showtitle" :style="{color:item.titleColor,width:itemHeight}">{{item.showtitle}}</p>
          </a>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
  /**
   * @Author: yqe@360shop.cn
   * @Date: 2020年07月08日15:11:21
   * @Last Modified by: yqe@360shop.cn
   * @Last Modified time: 2020年07月08日15:11:21
   */
  import { openPage } from '@/utils/utils'
  import Vue from 'vue'

  export default Vue.extend({
    name: 'index',
    data() {
      return {
        navList: [],
        styleObj: {},
        styleObj1: {},
        itemHeight: '',
        levelPadding: 0
      }
    },
    props: {
      tplItemData: {
        type: Object // 传入参数的类型
      }
    },
    watch: {
      tplItemData(val) {
        this.handleData(val)
      }
    },
    methods: {
      openLink(link){
        // console.log(link)
        openPage(link)
      },
      handleData(tplItemData) {
        this.navList = tplItemData.dataset
        if (!tplItemData.levelPadding) {
          this.levelPadding = 0
        } else {
          this.levelPadding = tplItemData.levelPadding
        }
        this.styleObj.padding = tplItemData.modulePadding + 'px ' + this.levelPadding + 'px'
        this.styleObj1.fontSize = tplItemData.fontSize + 'px'
        this.styleObj.background = tplItemData.backgroundColor ? tplItemData.backgroundColor : ''
      }
    },
    created() {
      // // console.log(this.tplItemData, 'imgNav')
      this.handleData(this.tplItemData)
    },
    mounted() {
      if (this.$refs.getWidth0) {
        this.$refs.getWidth0[0].$$getBoundingClientRect().then(res=>{
          this.itemHeight = res.width+'px'
        })
        // this.itemHeight = this.$refs.getWidth1[0].offsetWidth + 'px'
      }
    }
  })
</script>

<style lang='scss'>
.decorate-imgNav{
  .borderRadius{
    .style1{
      .layout-box1 a{ border-radius: 12px;}
      .layout-box2 a img{ border-radius: 12px;}
    }
    .style2 a{ border-radius: 12px;} 
  }

  width: 100%;
  // box-sizing: border-box;
  // padding: 0 25px;
  .img-nav-box{
    display: flex;
    .layout-box{
      flex: 1;
      width: 100%;
      display: flex;
    }
  }
  .style1{
    .layout-box1{
      a{
        flex: 1;
        position: relative;
        margin-right: 15px;
        overflow: hidden;
        &:last-child{
          margin-right: 0;
        }
        p{
          position: absolute;
          text-align: center;
          bottom: 0;
          font-size: 24px;
          line-height: 1.2;
          width: 100%;
          padding: 8px 0;
          // border-radius: 0 0 12px 12px;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
        &:last-child{
          margin-right:0;
        }
      }
    }
    .layout-box2{
      width: 100%;
      a{
        display: flex;
        flex-direction: column;
        flex: 1;
        // width: 128px;
        min-width: 128px;
        overflow: hidden;
        &.marginRight{
          margin-right: 14px;
          &:last-child{
            margin-right: 0;
          }
        }
        img{
          width: 100%;
          display: block;
          // border-radius: 12px;
        }
        p{
          text-align: center;
          font-size: 24px;
          line-height: 1.2;
          width: 100%;
          margin-top: 6px;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
      }
      // a{
      //   flex: 1;
      //   margin-right: 15px;
      //   width: 100;
      //   img{
      //     border-radius: 12px;
      //     width: 100%;
      //     height: 100%;
      //   }
      //   p{
      //     background: pink;
      //     text-align: center;
      //     font-size: 28px;
      //     width: 100%;
      //     overflow: hidden;
      //     white-space: nowrap;
      //     text-overflow: ellipsis;
      //   }
      // }
    }
  }
  .style2{
    flex-wrap: wrap;
    a{
      display: flex;
      align-items: center;
      width: 48%;
      padding-left: 6px;
      margin: 0 0 10px 10px ;
      background: #fff;
      height: 100px;
      box-sizing: border-box;
      em{
        width: 20%;
      }
      p{
        flex: 1;
        margin-left: 12px;
        font-size: 28px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
    }
  }
}
</style>
